/**org.mrlof.navigation.DefaultCellRAW
 * created on 2009-2-20	上午11:25:56
 */
package org.mrlof.navigation;

/**
 * @author thiswind (thiswind@gmail.com)
 *
 */
public class DefaultCellRAW<T> implements CellReaderAndWriter<T> {
	
	private T[][] matrix;
	
	private int localeX;
	private int loclaeY;
	
	private int rawX;
	private int rawY;
	
	private int movX;
	private int movY;
	
	private int size;
	
	/**
	 * @param matterMatrix
	 */
	public DefaultCellRAW(T[][] matrix) {
		this.matrix = matrix;
		
		this.localeX = -1;
		this.loclaeY = -1;
		
		this.rawX = -1;
		this.rawY = -1;
		
		this.movX = -1;
		this.movY = -1;
		
		this.size = this.matrix.length;
	}
	
	protected boolean validate(int x, int y) {
		return (0 <= x && x < this.size) && (0 <= y && y <= this.size);
	}
	
	/**
	 * 读取 (rawX, rawY) 位置的值
	 * @return 值
	 * @see this{@link #rawX}
	 * @see this{@link #rawY}
	 */
	protected T read() {
		if (this.validate(this.rawX, this.rawY)) {
			return this.matrix[this.rawX][this.rawY];
		} else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#compareAndSet(java.lang.Object, org.mrlof.navigation.CellComparer)
	 */
	@Override
	public boolean compareAndSet(T newValue, CompareCondition<T> condition) {
		T oldValue = this.readLocale();
		if (condition.compare(oldValue, newValue)) {
			try {
				this.setValue(newValue);
				return true;
			} catch (Exception ex) {
				ex.printStackTrace();
				return false;
			}
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#locate(int, int)
	 */
	@Override
	public boolean locate(int x, int y) {
		if (this.validate(x, y)) {
			this.localeX = x;
			this.loclaeY = y;
			
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#locate(org.mrlof.navigation.Point)
	 */
	@Override
	public boolean locate(Point p) {
		return this.locate(p.x, p.y);
	}
	
	protected boolean move() {
		if (this.validate(this.movX, this.movY)) {
			this.localeX = this.movX;
			this.loclaeY = this.movY;
			
			return true;
		} else {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToEast()
	 */
	@Override
	public boolean moveToEast() {
		this.movX = this.localeX + 1;
		this.movY = this.loclaeY;
		
		return this.move();
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToNorth()
	 */
	@Override
	public boolean moveToNorth() {
		this.movX = this.localeX;
		this.movY = this.loclaeY - 1;
		
		return this.move();
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToNorthEast()
	 */
	@Override
	public boolean moveToNorthEast() {
		this.movX = this.localeX + 1;
		this.movY = this.loclaeY - 1;
		
		return this.move();
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToNorthWest()
	 */
	@Override
	public boolean moveToNorthWest() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToSouth()
	 */
	@Override
	public boolean moveToSouth() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToSouthEast()
	 */
	@Override
	public boolean moveToSouthEast() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToSouthWest()
	 */
	@Override
	public boolean moveToSouthWest() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#moveToWest()
	 */
	@Override
	public boolean moveToWest() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readEast()
	 */
	@Override
	public T readEast() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readNorth()
	 */
	@Override
	public T readNorth() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readNorthEast()
	 */
	@Override
	public T readNorthEast() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readNorthWest()
	 */
	@Override
	public T readNorthWest() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readSouth()
	 */
	@Override
	public T readSouth() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readSouthEast()
	 */
	@Override
	public T readSouthEast() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readSouthWest()
	 */
	@Override
	public T readSouthWest() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readWest()
	 */
	@Override
	public T readWest() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#setValue(java.lang.T)
	 */
	@Override
	public boolean setValue(T value) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.mrlof.navigation.CellReaderAndWriter#readLocale()
	 */
	@Override
	public T readLocale() {
		this.rawX = this.localeX;
		this.rawY = this.loclaeY;
		
		return this.read();
	}
	
}
